package cn.exrick.xboot.cummunity.controller;

import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.Member;
import cn.exrick.xboot.cummunity.entity.CumAddress;
import cn.exrick.xboot.cummunity.service.ICumAddressService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
* <p>
    * 社区跑腿-地址  前端控制器
    * </p>
*
* @author xujiaxin
* @since 2024-08-26
*/
@RestController
@RequestMapping("/xboot/cumaddress")
public class CumAddressController {


    @Autowired
    private ICumAddressService cumAddressService;

    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 获取我的地址
     * @return
     */
    @GetMapping("getMyAddress")
    public Result getMyAddress() {
        QueryWrapper<CumAddress> wrapper = new QueryWrapper();
        wrapper.eq("create_user", securityUtil.getCurrMember().getId());
        wrapper.orderByDesc("create_time");
        List<CumAddress> list = cumAddressService.list(wrapper);
        return ResultUtil.data(list);
    }

    /**
     * 添加、编辑地址
 }
     * @param cumAddress
     * @return
     */
    @PostMapping("addOrEditAddress")
    public Result addOrEditAddress(CumAddress cumAddress) {
        if(cumAddress.getId() != null){
            cumAddressService.updateById(cumAddress);
        }else {
            Member m = securityUtil.getCurrMember();
            cumAddress.setCreateUser(m.getId());
            cumAddress.setCreateTime(new Date());
            cumAddress.setCreateUserName(m.getNickname());
            cumAddress.setCreateUserAvatar(m.getAvatar());
            cumAddressService.save(cumAddress);
        }
        return ResultUtil.data("操作成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("delete")
    public Result delete(Integer id) {
        cumAddressService.removeById(id);
        return ResultUtil.data("删除成功");
    }
}
